import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;



import javax.swing.JOptionPane;
import javax.swing.*;

//import java.util.*;

public class SaisieQuestion extends JDialog implements ActionListener {
	
	
	private Question question;
    private Reponse[] reponse=new Reponse[5] ;
    
    private Reponse rep=new Reponse();
    private Reponse rep1=new Reponse();
    private Reponse rep2=new Reponse();
    private Reponse rep3=new Reponse();
    private Reponse rep4=new Reponse();
    private Question[] tab=new Question[50] ;
    
    
    
    private static int nbQuestion=1;
  //  private int nbQCM=0;
    
    private SaisieQuestion boitequestion;
    
    private String auteur;
    private String nom;
    private boolean type;
    
    private QCM qcm;
    
    
    
    
    private JTextField jtf1=new JTextField("Sans Titre");
    private JTextField jtf2=new JTextField("Sans Titre");
    private JTextField jtf3=new JTextField("Sans Titre");
    private JTextField jtf4=new JTextField("Sans Titre");
    private JTextField jtf5=new JTextField("Sans Titre");
    private JTextField jtf6=new JTextField("Sans Titre");
  //  private JTextField jtf7=new JTextField("Sans Titre");
    
    private JLabel label1=new JLabel("Question: "+ nbQuestion);
    private JLabel label2=new JLabel("Choix:");
    private JLabel label3=new JLabel(" * ");
    private JLabel label4=new JLabel(" * ");
    private JLabel label5=new JLabel(" * ");
    private JLabel label6=new JLabel(" (*)'Champs obligatoires' ");
    private JLabel label7=new JLabel("   ");
    private JLabel label8=new JLabel("   ");
    private JLabel label9=new JLabel("   ");
    private JLabel label10=new JLabel("                            ");
    
    
    JOptionPane jop1;
    JOptionPane jop2;
    
    private JPanel panel=new JPanel();
    private JPanel panel3=new JPanel();
    private JPanel panel2=new JPanel();
    private JPanel panel4=new JPanel();
    private JPanel panel5=new JPanel();
    private JPanel panel6=new JPanel();
    private JPanel panel7=new JPanel();
    private JPanel panel8=new JPanel();
    private JPanel panel9=new JPanel();
    private JPanel panel10=new JPanel();
    private JPanel panel11=new JPanel();
    private JPanel panel12=new JPanel();
    
  	        
  
    private JRadioButton case1=new JRadioButton("Bonne");
    private JRadioButton case2=new JRadioButton("Mauvaise");
	
    private JRadioButton case3=new JRadioButton("Bonne");
    private JRadioButton case4=new JRadioButton("Mauvaise");
	
    private JRadioButton case5=new JRadioButton("Bonne");
    private JRadioButton case6=new JRadioButton("Mauvaise");
    
    private JRadioButton case7=new JRadioButton("Bonne");
    private JRadioButton case8=new JRadioButton("Mauvaise");
	
    private JRadioButton case9=new JRadioButton("Bonne");
    private JRadioButton case10=new JRadioButton("Mauvaise");
	
    
    private Bouton bouton1=new Bouton("Suivant");
    private Bouton bouton2=new Bouton("Annuler");
    private Bouton bouton3=new Bouton("Terminer"); 
	 
    private ButtonGroup bg= new ButtonGroup();
    private ButtonGroup bg2= new ButtonGroup();
    private ButtonGroup bg3= new ButtonGroup();
    private ButtonGroup bg4= new ButtonGroup();
    private ButtonGroup bg5= new ButtonGroup();
    
    private Box b=Box.createVerticalBox();
    
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    
    
    
    
    public SaisieQuestion(JFrame parent,String titre, boolean modal,String nom,String auteur,boolean type,Question [] tab)
    {
    	
    	super(parent,titre,modal);
    	this.nom=nom;
    	this.auteur=auteur;
    	this.type=type;
    	this.tab=new Question[tab.length];
        
    	for(int i=0;i<tab.length;i++)
    	{
    		this.tab[i]=tab[i];
    	}
    	
    	nbQuestion++;
    //	nbQCM=nbQuestion;
    	
    	
    	jtf1.setPreferredSize(new Dimension(450,30));
    	jtf2.setPreferredSize(new Dimension(200,30));
    	jtf3.setPreferredSize(new Dimension(200,30));
    	jtf4.setPreferredSize(new Dimension(200,30));
    	jtf5.setPreferredSize(new Dimension(200,30));
    	jtf6.setPreferredSize(new Dimension(200,30));
		
    	case1.setSelected(true);
    	case4.setSelected(true);
    	case6.setSelected(true);
    	case8.setSelected(true);
    	case10.setSelected(true);
	 	
    	
	 	 bg.add(case1);
		 bg.add(case2);
		 
		 bg2.add(case3);
		 bg2.add(case4);
		 
		 bg3.add(case5);
		 bg3.add(case6);
		 
		
		 bg4.add(case7);
		 bg4.add(case8);
		 
		 
		 bg5.add(case9);
		 bg5.add(case10);
		 
		 
		 
		 this.setSize(600,350);
 		this.setLocationRelativeTo(null);
 		this.setResizable(false);
 		
 		panel.setLayout(new FlowLayout());
		panel2.setLayout(new FlowLayout());
		panel3.setLayout(new FlowLayout());
		panel4.setLayout(new FlowLayout());
		panel5.setLayout(new FlowLayout());
		panel6.setLayout(new FlowLayout());
		panel7.setLayout(new FlowLayout());
		panel8.setLayout(new FlowLayout());
		panel9.setLayout(new FlowLayout());
		panel10.setLayout(new FlowLayout());
		panel11.setLayout(new FlowLayout());
		panel12.setLayout(new FlowLayout());
		
 		
		panel.add(label1);
		panel2.add(jtf1);
		panel2.add(label3);
		
		b.add(label2);
		
		panel4.add(label4);
		panel4.add(jtf2);
		panel4.add(case1);
		panel4.add(case2);
		
		panel5.add(label5);
		panel5.add(jtf3);
		panel5.add(case3);
		panel5.add(case4);
		
		
		panel6.add(label7);
		panel6.add(jtf4);
		panel6.add(case5);
		panel6.add(case6);
		
		
		panel7.add(label8);
		panel7.add(jtf5);
		panel7.add(case7);
		panel7.add(case8);
		
		
		panel8.add(label9);
		panel8.add(jtf6);
		panel8.add(case9);
		panel8.add(case10);
		
		
		panel9.add(label6);
		panel9.add(label10);
		
		panel9.add(bouton1);
		panel9.add(bouton2);
		panel9.add(bouton3);
		
		b.add(panel4);
		b.add(panel5);
		b.add(panel6);
		b.add(panel7);
		b.add(panel8);
		b.add(panel9);
		
		
		panel10.add(panel);
		panel10.add(panel2);
		panel10.add(b);
	
		this.setContentPane(panel10);
		
		bouton1.addActionListener(this);
		bouton2.addActionListener(this);
		bouton3.addActionListener(this);
		
		
		
		this.setVisible(true);
        
		
			
    }
    public void actionPerformed(ActionEvent arg0) {
		   if (arg0.getSource()==bouton2)
		   {
			    this.setVisible(false);
			   nbQuestion=1;
			   System.out.println("yassine");
		   }
		   
		   if (arg0.getSource()==bouton1)
		   {
			   if (jtf1.getText().equals("Sans Titre") || jtf1.getText().equals("") || jtf2.getText().equals("Sans Titre") ||
			   jtf2.getText().equals("") || jtf3.getText().equals("Sans Titre") || jtf3.getText().equals(""))
			   {
				 jop1=new JOptionPane();
				 jop1.showMessageDialog(null,"Veuillez remplir tous les champs obligatoires !!","Erreur",
				 JOptionPane.WARNING_MESSAGE);
				 
			   }
			   else
			   { 
			       String a;
			       a=jtf1.getText();
			       
				   rep.setRep(jtf2.getText());
				   rep.setRepJuste(case1.isSelected());
				   reponse[0]=rep;
				   
				   
				   rep1.setRep(jtf3.getText());
				   rep1.setRepJuste(case3.isSelected());
				   reponse[1]=rep1;
				   
				   rep2.setRep(jtf4.getText());
				   rep2.setRepJuste(case5.isSelected());
				   reponse[2]=rep2;
				   
				   
				   
				   rep3.setRep(jtf5.getText());
				   rep3.setRepJuste(case7.isSelected());
				   reponse[3]=rep3;
				   
				   
				   
				   rep4.setRep(jtf6.getText());
				   rep4.setRepJuste(case9.isSelected());
				   reponse[4]=rep4;
				   
				   
				   
				   question=new Question(a,reponse);
				   tab[nbQuestion-2]=question;
				
				   this.setVisible(false);  
				   boitequestion=new SaisieQuestion(null,"Les questions",true,nom,auteur,type,tab);
		  	
			   }
			   
			   
			 
	    }
		   if (arg0.getSource()==bouton3)
		   {
			   if (jtf1.getText().equals("Sans Titre") || jtf1.getText().equals("") || jtf2.getText().equals("Sans Titre") ||
					   jtf2.getText().equals("") || jtf3.getText().equals("Sans Titre") || jtf3.getText().equals(""))
					   {
						 jop2=new JOptionPane();
						 jop2.showMessageDialog(null,"Veuillez remplir tous les champs obligatoires !!","Erreur",
						 JOptionPane.WARNING_MESSAGE);
						 
					   }
					   else
					   { 
					       String a;
					       a=jtf1.getText();
					       
						   rep.setRep(jtf2.getText());
						   rep.setRepJuste(case1.isSelected());
						   reponse[0]=rep;
						   
						   
						   rep1.setRep(jtf3.getText());
						   rep1.setRepJuste(case3.isSelected());
						   reponse[1]=rep1;
						   
						   rep2.setRep(jtf4.getText());
						   rep2.setRepJuste(case5.isSelected());
						   reponse[2]=rep2;
						   
						   
						   
						   rep3.setRep(jtf5.getText());
						   rep3.setRepJuste(case7.isSelected());
						   reponse[3]=rep3;
						   
						   
						   
						   rep4.setRep(jtf6.getText());
						   rep4.setRepJuste(case9.isSelected());
						   reponse[4]=rep4;
						   
						   
						   
						   
						   question=new Question(a,reponse);
						   tab[nbQuestion-2]=question;
						   
						  // JFileChooser ch=new JFileChooser();
						  // ch.setVisible(true);
						   
						  
						  // qcm=new QCM(auteur,nom,type,tab,nbQuestion-1);
						  try 
						  {
						   
							   oos=new ObjectOutputStream(new BufferedOutputStream( new FileOutputStream (new File("test.txt"))));
						    
							   
							   
							   oos.writeObject(new QCM(auteur,nom,type,tab,nbQuestion-1));
						       oos.close();
						       
						  //    ois=new ObjectInputStream(new BufferedInputStream( new FileInputStream (new File("test.txt"))));
						       
						    //   try
						     //  {
						    	    // qcm=((QCM)ois.readObject());
						    	    /*    System.out.println("l'auteur"+qcm.getAuteurQCM());
									   
									   System.out.println("nom"+qcm.getNomQCM());
									   
									   System.out.println("type"+qcm.isTypeQCM());
									   
									   for(int i=0;i<qcm.getNombreQuestion();i++)
									   {
										   System.out.println("les questions"+qcm.getTab(i).getQuestion());
										   for(int j=0;j<5;j++)
										   {
											   System.out.println("les reponses"+qcm.getTab(i).getReponse(j).getRep());
											   System.out.println("les reponses + verif"+qcm.getTab(i).getReponse(j).isRepJuste());
											   
										   }
										   
										      
									   }*/
									
						    	     
							    	
							    
								
						    	  //System.out.println(((QCM)ois.readObject()).toString());
						    /*   }catch (ClassNotFoundException e){
						    	   e.printStackTrace();
						       }*/
						       //ois.close();
						       
						  }catch (FileNotFoundException e){
						  e.printStackTrace();
						  }catch(IOException e){
							  e.printStackTrace();
								  
						  }
						 	
						  
					   } 
			   }
						   
						/*   System.out.println("l'auteur"+qcm.getAuteurQCM());
						   
						   System.out.println("nom"+qcm.getNomQCM());
						   
						   System.out.println("type"+qcm.isTypeQCM());
						   
						   for(int i=0;i<qcm.getNombreQuestion();i++)
						   {
							   System.out.println("les questions"+qcm.getTab(i).getQuestion());
							   for(int j=0;j<5;j++)
							   {
								   System.out.println("les reponses"+qcm.getTab(i).getReponse(j).getRep());
								   System.out.println("les reponses + verif"+qcm.getTab(i).getReponse(j).isRepJuste());
								   
							   }
							   
							      
						   }*/
						   
		   this.setVisible(false);  
		  	
						   
						  
					   }
          
    
    
    		  
			    }


	   
	

    
    

    


